class ObstalsList {
    constructor(viewer,url) {
        this._viewer = viewer;
        this._region = [];
        this._obstals = [];
        this._gridPri = null;
        this._gridOutlinePri = null;
        this._url = url
    }

    get Obstals() {
        return this._obstals;
    }

    async fetchObstalRegions() {
        const serverurl = this._url + "/obsregions";
        const res = await fetch(serverurl);
        const obss = await res.json();
        this._obstals = obss;
        // just create all entities for once
        for (let i = 0; i < obss.length; i++) {
            for (const region of obss[i].region) {
                const coords = region.coords;
                const cartes = Cesium.Cartesian3.fromDegreesArray(coords);
                const en = this._viewer.entities.add({
                    polygon: {
                        hierarchy: new Cesium.PolygonHierarchy(cartes),
                        height: region.heights[0],
                        extrudedHeight: region.heights[1],
                        outline: true,
                        outlineColor: Cesium.Color.YELLOW,
                        outlineWidth: 2,
                        material: region.cost === 0 ? Cesium.Color.RED.withAlpha(0.5) : Cesium.Color.YELLOW.withAlpha(0.5)
                    }
                });
                en.show = false;
                en.obsid = obss[i].obsid;
                this._region.push(en);
            }
        }
    }

    async ViewGrid(obsid, vis) {
        this.clearGrid();
        if (!vis) {
            return;
        }
        const scene = this._viewer.scene;

        const url = `${this._url}/obsregion/${obsid}`;
        const res = await fetch(url);
        const grids = await res.json();

        const inses = [];
        const outlineinses = [];
        for (const g of grids) {
            const rectgeom = new Cesium.RectangleGeometry({
                ellipsoid: Cesium.Ellipsoid.default,
                rectangle: Cesium.Rectangle.fromDegrees(g.extent[0], g.extent[1], g.extent[2], g.extent[3]),
                height: g.heights[0],
                extrudedHeight: g.heights[1]
            });
            inses.push(new Cesium.GeometryInstance({
                geometry: rectgeom,
                attributes: {
                    color: g.cost === 0 ?
                        new Cesium.ColorGeometryInstanceAttribute(1.0, 0.0, 0.0, 0.3)
                        : new Cesium.ColorGeometryInstanceAttribute(1.0, 0.5, 0.3, 0.3)
                }
            }));
            const rectoutline = new Cesium.RectangleOutlineGeometry({
                ellipsoid: Cesium.Ellipsoid.default,
                rectangle: Cesium.Rectangle.fromDegrees(g.extent[0], g.extent[1], g.extent[2], g.extent[3]),
                height: g.heights[0],
                extrudedHeight: g.heights[1],
            });
            outlineinses.push(new Cesium.GeometryInstance({
                geometry: rectoutline,
                id: "lxsgrid",
                attributes: {
                    color: new Cesium.ColorGeometryInstanceAttribute(1.0, 1.0, 0.0, 1.0)
                }
            }));
        }

        this._gridPri = new Cesium.Primitive({
            geometryInstances: inses,
            appearance: new Cesium.PerInstanceColorAppearance({ flat: true }),
            allowPicking: false,
        });
        this._gridOutlinePri = new Cesium.Primitive({
            geometryInstances: outlineinses,
            appearance: new Cesium.PerInstanceColorAppearance({ flat: true }),
            allowPicking: false,
        });
        this._gridOutlinePri.name = "lxs";

        scene.primitives.add(this._gridPri);
        scene.primitives.add(this._gridOutlinePri);
    }

    ViewRegion(obsid, vis) {
        const region = this._region.filter(r => r.obsid === obsid);
        if (region.length === 0) return;
        region.forEach(r => r.show = vis)
    }

    clearGrid() {
        const scene = this._viewer.scene;
        if (this._gridOutlinePri) {
            scene.primitives.remove(this._gridOutlinePri);
            this._gridOutlinePri = null;
        }
        if (this._gridPri) {
            scene.primitives.remove(this._gridPri);
            this._gridPri = null;
        }
    }

    async delteRegion(obsid) {
        const url = `${this._url}/delobstal/${obsid}`;
        const res = await fetch(url);
        const response = await res.json();
        return response;
    }
}

export { ObstalsList }